import { DataTypeList, FunctionTypeList, ReadWriteTypeList, getEnumLabel } from '@/constants';

/**
 * 功能点详情查看组件
 */
interface ModelDetailViewProps {
  data: any;
}

const ModelDetailView = ({ data }: ModelDetailViewProps) => {
  // 获取显示文本的辅助函数
  const getDisplayText = (value: any, defaultText = '暂无数据') => {
    if (value === null || value === undefined || value === '') {
      return defaultText;
    }
    return value;
  };

  // 获取实际的数据类型
  const getActualDataType = (dataType: any) => {
    if (typeof dataType === 'object' && dataType?.type) {
      return dataType.type;
    }
    return dataType;
  };

  // 获取数据类型的配置信息
  const getDataTypeSpecs = (record: any) => {
    // 优先从 dataType 对象中获取 specs
    if (typeof record.dataType === 'object' && record.dataType?.specs) {
      return record.dataType.specs;
    }
    // 其次从 originalDataType 中获取 specs
    if (
      record.originalDataType &&
      typeof record.originalDataType === 'object' &&
      record.originalDataType.specs
    ) {
      return record.originalDataType.specs;
    }
    // 最后从 record 的直接属性中获取
    return {
      min: record.min,
      max: record.max,
      step: record.step,
      unit: record.unit,
      length: record.dataLen,
      true: record.boolT,
      false: record.boolF,
    };
  };

  // 渲染参数列表为表格形式
  const renderParamList = (paramList: any[], title: string) => {
    if (!paramList || paramList.length === 0) {
      return (
        <ADescriptions.Item label={title} span={2}>
          <span style={{ color: '#999' }}>暂无参数</span>
        </ADescriptions.Item>
      );
    }

    const columns = [
      {
        title: '参数名称',
        dataIndex: 'name',
        key: 'name',
        render: (text: string) => getDisplayText(text),
      },
      {
        title: '标识符',
        dataIndex: 'identifier',
        key: 'identifier',
        render: (text: string) => getDisplayText(text),
      },
      {
        title: '数据类型',
        dataIndex: 'dataType',
        key: 'dataType',
        render: (text: any) => {
          const actualType = getActualDataType(text);
          return <ATag color="blue">{getEnumLabel(DataTypeList, actualType, actualType)}</ATag>;
        },
      },
    ];

    return (
      <ADescriptions.Item label={title} span={2}>
        <ATable
          columns={columns}
          dataSource={paramList.map((item, index) => ({ ...item, key: index }))}
          pagination={false}
          size="small"
          bordered
          style={{ marginTop: 8 }}
        />
      </ADescriptions.Item>
    );
  };

  return (
    <div style={{ padding: '16px 0' }}>
      <ADescriptions
        title="功能点详情"
        bordered
        column={2}
        size="middle"
        labelStyle={{
          width: '120px',
          backgroundColor: '#fafafa',
          fontWeight: 'bold',
          color: '#333',
          fontSize: '14px',
        }}
        contentStyle={{
          backgroundColor: '#fff',
          minHeight: '40px',
          display: 'flex',
          alignItems: 'center',
          fontSize: '14px',
          color: '#666',
        }}
      >
        {/* 基本信息 */}
        <ADescriptions.Item label="功能名称">{getDisplayText(data.name)}</ADescriptions.Item>
        <ADescriptions.Item label="标识符">{getDisplayText(data.identifier)}</ADescriptions.Item>

        <ADescriptions.Item label="功能类型">
          <ATag color="blue">{getEnumLabel(FunctionTypeList, data.type, data.type)}</ATag>
        </ADescriptions.Item>

        {/* 属性类型特有字段 */}
        {data.type === 'properties' && (
          <>
            <ADescriptions.Item label="数据类型">
              <ATag color="green">
                {getEnumLabel(
                  DataTypeList,
                  getActualDataType(data.dataType),
                  getActualDataType(data.dataType)
                )}
              </ATag>
            </ADescriptions.Item>

            <ADescriptions.Item label="读写类型">
              <ATag color="orange">
                {getEnumLabel(ReadWriteTypeList, data.accessMode, data.accessMode)}
              </ATag>
            </ADescriptions.Item>

            {/* 根据数据类型显示配置信息 */}
            {(() => {
              const actualDataType = getActualDataType(data.dataType);
              const specs = getDataTypeSpecs(data);

              // 数值类型的配置
              if (['int32', 'int64', 'float', 'double'].includes(actualDataType)) {
                return (
                  <>
                    <ADescriptions.Item label="数值范围">
                      {getDisplayText(specs.min)} ~ {getDisplayText(specs.max)}
                    </ADescriptions.Item>
                    <ADescriptions.Item label="步长">
                      {getDisplayText(specs.step)}
                    </ADescriptions.Item>
                    <ADescriptions.Item label="单位">
                      {getDisplayText(specs.unit)}
                    </ADescriptions.Item>
                  </>
                );
              }

              // 字符串类型的配置
              if (actualDataType === 'string') {
                return (
                  <>
                    <ADescriptions.Item label="数据长度">
                      {getDisplayText(specs.length || data.dataLen)}
                    </ADescriptions.Item>
                    <ADescriptions.Item label="长度单位">
                      {getDisplayText(data.dataLenUnit)}
                    </ADescriptions.Item>
                  </>
                );
              }

              // 布尔类型的配置
              if (actualDataType === 'bool') {
                return (
                  <>
                    <ADescriptions.Item label="True值">
                      {getDisplayText(specs.true || data.boolT)}
                    </ADescriptions.Item>
                    <ADescriptions.Item label="False值">
                      {getDisplayText(specs.false || data.boolF)}
                    </ADescriptions.Item>
                  </>
                );
              }

              // 枚举类型的配置
              if (actualDataType === 'enum' && data.domains && data.domains.length > 0) {
                return (
                  <ADescriptions.Item label="枚举项" span={2}>
                    <div>
                      {data.domains.map((domain: any, index: number) => (
                        <ATag key={index} style={{ margin: '2px 4px 2px 0' }}>
                          {domain.value}: {domain.desc}
                        </ATag>
                      ))}
                    </div>
                  </ADescriptions.Item>
                );
              }

              return null;
            })()}
          </>
        )}

        {/* 服务类型特有字段 */}
        {data.type === 'services' && (
          <>
            <ADescriptions.Item label="调用方式">
              <ATag color={data.callType === 'sync' ? 'green' : 'orange'}>
                {data.callType === 'sync' ? '同步' : '异步'}
              </ATag>
            </ADescriptions.Item>
            <ADescriptions.Item label="">-</ADescriptions.Item>
          </>
        )}

        <ADescriptions.Item label="备注" span={2}>
          {getDisplayText(data.remark)}
        </ADescriptions.Item>
      </ADescriptions>

      {/* 事件类型的参数列表 */}
      {data.type === 'events' && (
        <div style={{ marginTop: 24 }}>
          <ADescriptions
            bordered
            column={1}
            size="middle"
            labelStyle={{
              width: '120px',
              backgroundColor: '#fafafa',
              fontWeight: 'bold',
              color: '#333',
              fontSize: '14px',
            }}
            contentStyle={{
              backgroundColor: '#fff',
              fontSize: '14px',
              color: '#666',
            }}
          >
            {renderParamList(data.paramList, '输出参数')}
          </ADescriptions>
        </div>
      )}

      {/* 服务类型的输入输出参数 */}
      {data.type === 'services' && (
        <div style={{ marginTop: 24 }}>
          <ADescriptions
            bordered
            column={1}
            size="middle"
            labelStyle={{
              width: '120px',
              backgroundColor: '#fafafa',
              fontWeight: 'bold',
              color: '#333',
              fontSize: '14px',
            }}
            contentStyle={{
              backgroundColor: '#fff',
              fontSize: '14px',
              color: '#666',
            }}
          >
            {renderParamList(data.inputData, '输入参数')}
            {renderParamList(data.outputData, '输出参数')}
          </ADescriptions>
        </div>
      )}
    </div>
  );
};

export default ModelDetailView;
